package com.ruoyi.manage.enterprise.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ObjUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StrUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.manage.enterprise.domain.PcProductBaseInfo;
import com.ruoyi.manage.enterprise.domain.PcProductCollection;
import com.ruoyi.manage.enterprise.domain.vo.PcProductCollectionVo;
import com.ruoyi.manage.enterprise.service.IPcProductBaseInfoService;
import com.ruoyi.manage.enterprise.service.IPcProductCollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;
/**
 * 产品收藏Controller
 *
 * @author ruoyi
 * @date 2025-08-26
 */
@Api(tags="产品收藏控制器")
@RestController
@RequestMapping("/manage/collection" )
public class PcProductCollectionController extends BaseController {
    /**
    * 配置表别名 默认空
    */
    private final static String TABLE_PREFIX=StrUtil.initTableAlias(null);
    @Autowired
    private IPcProductCollectionService pcProductCollectionService;
    
    @Autowired
    private IPcProductBaseInfoService iPcProductBaseInfoService;

    /**
     * 查询产品收藏列表
     */
    @ApiOperation(value = "查询产品收藏",notes = "产品收藏管理")
    @GetMapping("/list" )
    public TableDataInfo list(PcProductCollectionVo pcProductCollection,QueryWrapper<PcProductCollection> queryWrapper) {
        setQueryCondition(pcProductCollection,queryWrapper);
       
        List<PcProductCollection> productCollections = pcProductCollectionService.lambdaQuery().eq(PcProductCollection::getUserId, SecurityUtils.getUserId()).list();
        List<Long> productIds = productCollections.stream().map(m -> m.getProductId()).collect(Collectors.toList());
        startPage();
        List<PcProductBaseInfo> list = iPcProductBaseInfoService.lambdaQuery().in(PcProductBaseInfo::getId, productIds).list();
        return getDataTable(list);
    }
    private void setQueryCondition(PcProductCollectionVo pcProductCollection, QueryWrapper<PcProductCollection> queryWrapper) {
        if(pcProductCollection.getProductId() != null){
            queryWrapper.eq(TABLE_PREFIX+"product_id" ,pcProductCollection.getProductId());
        }
        if(StrUtil.isNotBlank(pcProductCollection.getCollectStatus())){
            queryWrapper.eq(TABLE_PREFIX+"collect_status" ,pcProductCollection.getCollectStatus());
        }
        if(pcProductCollection.getUserId() != null){
            queryWrapper.eq(TABLE_PREFIX+"user_id" ,pcProductCollection.getUserId());
        }
        if(pcProductCollection.getDeleted() != null){
            queryWrapper.eq(TABLE_PREFIX+"deleted" ,pcProductCollection.getDeleted());
        }
        if(StrUtil.isNotBlank(pcProductCollection.getVersion())){
            queryWrapper.eq(TABLE_PREFIX+"version" ,pcProductCollection.getVersion());
        }
        queryWrapper.orderByDesc(TABLE_PREFIX+"create_time");
    }

    /**
     * 导出产品收藏列表
     */
    @PreAuthorize("@ss.hasPermi('manage:collection:export')" )
    @PostMapping("/export" )
    public void export(HttpServletResponse response, PcProductCollectionVo pcProductCollection,QueryWrapper<PcProductCollection> queryWrapper) {
        if (CollUtil.isEmpty( pcProductCollection.getIds())){
            setQueryCondition(pcProductCollection,queryWrapper);
        }else {
            queryWrapper.in(TABLE_PREFIX+"id",pcProductCollection.getIds());
        }
        List<PcProductCollection> list = pcProductCollectionService.list(queryWrapper);
        ExcelUtil<PcProductCollection> util = new ExcelUtil<PcProductCollection>(PcProductCollection. class);
        util.exportExcel(response,list, "collection");
    }

    /**
     * 获取产品收藏详细信息
     */
    @ApiOperation(value = "获取产品收藏",notes = "产品收藏管理")
    @PreAuthorize("@ss.hasPermi('manage:collection:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(pcProductCollectionService.queryGetById(id));
    }

    /**
     * 新增产品收藏
     */
    @ApiOperation(value = "新增产品收藏",notes = "产品收藏管理")
    @PreAuthorize("@ss.hasPermi('manage:collection:add')" )
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@Validated @RequestBody PcProductCollection pcProductCollection) {
        return toAjax(pcProductCollectionService.addPcProductCollection(pcProductCollection) ? 1 : 0);
    }

    /**
     * 修改产品收藏
     */
    @ApiOperation(value = "修改产品收藏",notes = "产品收藏管理")
    @PreAuthorize("@ss.hasPermi('manage:collection:edit')" )
    @PutMapping
    public AjaxResult edit(@RequestBody PcProductCollection pcProductCollection) {
        return toAjax(pcProductCollectionService.updatePcProductCollection(pcProductCollection) ? 1 : 0);
    }

    @PostMapping("/collect")
    public AjaxResult collect(@RequestBody PcProductCollection pcProductCollection){

        PcProductCollection one = pcProductCollectionService.lambdaQuery().eq(PcProductCollection::getProductId, pcProductCollection.getProductId()).eq(PcProductCollection::getUserId, SecurityUtils.getUserId()).one();
        if(ObjUtil.isNotNull(one)){
//            one.setCollectStatus(pcProductCollection.getCollectStatus());
            return toAjax(pcProductCollectionService.removeById(one.getId())) ;
        }
        pcProductCollection.setUserId(SecurityUtils.getUserId());
        return toAjax(pcProductCollectionService.addPcProductCollection(pcProductCollection));

    }

    /**
     * 删除产品收藏
     */
    @ApiOperation(value = "删除产品收藏",notes = "产品收藏管理")
    @PreAuthorize("@ss.hasPermi('manage:collection:remove')" )
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pcProductCollectionService.deletePcProductCollectionByIds(ids) ? 1 : 0);
    }
}
